<template>
    <view class="wo-page u-padding-20">
        <view class="wo-page-body u-padding-20">
            <data-form :form-model="personDetail" :form-config="personFieldConfig" ref="personFieldConfig"></data-form>
            <view class="wo-panel-foot u-padding-top-20"><u-button type="success" shape="circle" @click="personSubmitFunc">提交</u-button></view>
        </view>
        <u-toast ref="uToast" />
    </view>
</template>

<script>
import DataForm from '@/components/data-form';
export default {
    data() {
        return {
            wxUserDetail: {},
            personDetail: {},
            personFieldConfig: {
                name: {
                    field: 'name',
                    label: '受种人',
                    form_show: true,
                    form_type: 'input-text',
                    form_placeholder: '请输入受种人姓名'
                },
                id_number: {
                    field: 'id_number',
                    label: '身份证号',
                    form_show: true,
                    form_type: 'input-text',
                    form_placeholder: '请输入身份证号码'
                },
                wish: {
                    field: 'wish',
                    label: '接种意愿',
                    initFunc(viewModel) {
                        this.resultFunc(viewModel.personDetail, viewModel.personFieldConfig);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel, formConfig) {
						// 处理关联字段
						formConfig.time_1.form_show = formModel[this.field] == 2 ? true : false;
						formConfig.time_2.form_show = formModel[this.field] == 3 ? true : false;
						formConfig.wish_notice.form_show = formModel[this.field] < 3 ? true : false;
                        if (formModel[this.field]) {
                            for (let index in this.form_select_option) {
                                if (this.form_select_option[index].value == formModel[this.field]) {
                                    this.form_result = this.form_select_option[index].label;
                                    break;
                                }
                            }
                        }
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        // 选中处理
                        for (let index in this.form_select_option) {
                            if (this.form_select_option[index].value == formModel[this.field]) {
                                this.form_value[0] = parseInt(index);
                                break;
                            }
                        }
                    },
                    form_show: true,
                    form_type: 'select-single-column',
                    form_placeholder: '请选择接种意愿',
                    form_select_option: [
                        {
                            value: 1,
                            label: '愿意接种'
                        },
                        {
                            value: 2,
                            label: '第一针已接种'
                        },
                        {
                            value: 3,
                            label: '第二针已接种'
                        },
                        {
                            value: 4,
                            label: '不愿接种'
                        }
                    ],
                    // 打开选择器
                    form_select_open: false,
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: [0],
                    form_select_confirm(event, formModel, formConfig) {
                        // 数据赋值
                        formModel[this.field] = event[0].value;
                        this.resultFunc(formModel, formConfig);
                        this.valueFunc(formModel);
                    }
                },
                time_1: {
                    field: 'time_1',
                    label: '接种时间',
                    async initFunc(viewModel) {
                        this.resultFunc(viewModel.personDetail);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel) {
                        this.form_result = formModel[this.field];
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        
                    },
                    form_show: false,
                    form_type: 'select-calendar-date',
                    form_placeholder: '请选择第一针接种时间',
                    // 打开选择器
                    form_select_open: false,
                    form_select_title: '第一针接种时间',
                    form_start_year: '2020',
                    form_end_year: '2021',
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: '',
                    form_select_confirm(event, formModel) {
                        // 数据赋值
                        formModel[this.field] = event.result;
                        this.resultFunc(formModel);
                        this.valueFunc(formModel);
                    }
                },
                time_2: {
                    field: 'time_2',
                    label: '接种时间',
                    async initFunc(viewModel) {
                        this.resultFunc(viewModel.personDetail);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel) {
                        this.form_result = formModel[this.field];
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        
                    },
                    form_show: false,
                    form_type: 'select-calendar-date',
                    form_placeholder: '请选择第二针接种时间',
                    // 打开选择器
                    form_select_open: false,
                    form_select_title: '第二针接种时间',
                    form_start_year: '2020',
                    form_end_year: '2021',
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: '',
                    form_select_confirm(event, formModel) {
                        // 数据赋值
                        formModel[this.field] = event.result;
                        this.resultFunc(formModel);
                        this.valueFunc(formModel);
                    }
                },
                wish_notice: {
                    field: 'wish_notice',
                    label: '通知接种',
                    initFunc(viewModel) {
                        this.resultFunc(viewModel.personDetail);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel) {
                        if (formModel[this.field]) {
                            for (let index in this.form_select_option) {
                                if (this.form_select_option[index].value == formModel[this.field]) {
                                    this.form_result = this.form_select_option[index].label;
                                    break;
                                }
                            }
                        }
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        // 选中处理
                        for (let index in this.form_select_option) {
                            if (this.form_select_option[index].value == formModel[this.field]) {
                                this.form_value[0] = parseInt(index);
                                break;
                            }
                        }
                    },
                    form_show: false,
                    form_type: 'select-single-column',
                    form_placeholder: '请选择可通知接种的时段',
                    form_select_option: [
                        {
                            value: 1,
                            label: '随时都可以'
                        },
                        {
                            value: 2,
                            label: '周末及节假日'
                        },
                        {
                            value: 3,
                            label: '工作日接种'
                        }
                    ],
                    // 打开选择器
                    form_select_open: false,
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: [0],
                    form_select_confirm(event, formModel, formConfig) {
                        // 数据赋值
                        formModel[this.field] = event[0].value;
                        this.resultFunc(formModel);
                        this.valueFunc(formModel);
                    }
                },
				abroad: {
                    field: 'abroad',
                    label: '是否出国',
                    initFunc(viewModel) {
                        this.resultFunc(viewModel.personDetail);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel) {
                        if (formModel[this.field]) {
                            for (let index in this.form_select_option) {
                                if (this.form_select_option[index].value == formModel[this.field]) {
                                    this.form_result = this.form_select_option[index].label;
                                    break;
                                }
                            }
                        }
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        // 选中处理
                        for (let index in this.form_select_option) {
                            if (this.form_select_option[index].value == formModel[this.field]) {
                                this.form_value[0] = parseInt(index);
                                break;
                            }
                        }
                    },
                    form_show: true,
                    form_type: 'select-single-column',
                    form_placeholder: '请选择近期是否出国',
                    form_select_option: [
                        {
                            value: 1,
                            label: '是'
                        },
                        {
                            value: 2,
                            label: '否'
                        }
                    ],
                    // 打开选择器
                    form_select_open: false,
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: [0],
                    form_select_confirm(event, formModel, formConfig) {
                        // 数据赋值
                        formModel[this.field] = event[0].value;
                        // 处理关联字段
                        formConfig.time_1.form_show = formModel[this.field] == 2 ? true : false;
                        formConfig.time_2.form_show = formModel[this.field] == 3 ? true : false;
                        this.resultFunc(formModel);
                        this.valueFunc(formModel);
                    }
                },
                type_id: {
                    field: 'type_id',
                    label: '行业类别',
                    async initFunc(viewModel) {
                        // 获取区域选择数据
                        let typeData = await viewModel.$u.api.typeSelectData();
                        if (!typeData) {
                            return false;
                        }
                        this.form_select_option = typeData;
                        this.resultFunc(viewModel.personDetail);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel) {
                        if (formModel[this.field]) {
                            for (let index in this.form_select_option) {
                                if (this.form_select_option[index].value == formModel[this.field]) {
                                    this.form_result = this.form_select_option[index].label;
                                    break;
                                }
                            }
                        }
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        // 选中处理
                        for (let index in this.form_select_option) {
                            if (this.form_select_option[index].value == formModel[this.field]) {
                                this.form_value[0] = parseInt(index);
                                break;
                            }
                        }
                    },
                    form_show: true,
                    form_type: 'select-single-column',
                    form_placeholder: '请选择行业类别',
                    form_select_option: [],
                    // 打开选择器
                    form_select_open: false,
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: [0],
                    form_select_confirm(event, formModel) {
                        // 数据赋值
                        formModel[this.field] = event[0].value;
                        this.resultFunc(formModel);
                        this.valueFunc(formModel);
                    }
                },
                area_id: {
                    field: 'area_id',
                    label: '所属社区',
                    // 初始化
                    async initFunc(viewModel) {
                        // 获取区域选择数据
                        let areaData = await viewModel.$u.api.areaSelectData();
                        if (!areaData) {
                            return false;
                        }
                        this.form_select_option = areaData;
                        this.resultFunc(viewModel.personDetail);
                        this.valueFunc(viewModel.personDetail);
                    },
                    // 结果处理
                    resultFunc(formModel) {
                        for (let index in this.form_select_option) {
                            if (this.form_select_option[index].value == formModel[this.field]) {
                                this.form_result = this.form_select_option[index].label;
                                break;
                            }
                            for (let _index in this.form_select_option[index].children) {
                                if (this.form_select_option[index].children[_index].value == formModel[this.field]) {
                                    this.form_result = this.form_select_option[index].label + ',' + this.form_select_option[index].children[_index].label;
                                    break;
                                }
                            }
                        }
                    },
                    // 赋值处理
                    valueFunc(formModel) {
                        // 选中处理
                        for (let index in this.form_select_option) {
                            if (this.form_select_option[index].value == formModel[this.field]) {
                                this.form_value[0] = parseInt(index);
                                this.form_value[1] = 0;
                                break;
                            }
                            for (let _index in this.form_select_option[index].children) {
                                if (this.form_select_option[index].children[_index].value == formModel[this.field]) {
                                    this.form_value[0] = parseInt(index);
                                    this.form_value[1] = parseInt(_index);
                                    break;
                                }
                            }
                        }
                    },
                    form_show: true,
                    form_type: 'select-muitl-column-auto',
                    form_placeholder: '请选择所属区域',
                    form_select_option: [],
                    // 打开选择器
                    form_select_open: false,
                    // 选择结果
                    form_result: '',
                    // 选择值
                    form_value: [0, 0],
                    form_select_confirm(event, formModel) {
                        formModel[this.field] = event[1].value || event[0].value;
                        // 展示处理
                        this.resultFunc(formModel);
                        // 值处理
                        this.valueFunc(formModel);
                    }
                },
                address: {
                    field: 'address',
                    label: '居住地址',
                    form_show: true,
                    form_type: 'input-text',
                    form_placeholder: '请输入小区楼栋门牌号'
                },
            }
        };
    },
    components: {
        DataForm
    },
    methods: {
        // 页面初始化
        async initPageFunc(option) {
			let id = option.id || 0;
			if (!id) {
				return false;
			}
			let personDetail = await this.$u.api.planPersonDetail({id: id});
			if (personDetail) {
				this.personDetail = personDetail;
				// 时间处理
				if (this.personDetail.time_1) {
					this.personDetail.time_1 = this.$dayjs.unix(this.personDetail.time_1).format('YYYY-MM-DD');
				} else {
					this.personDetail.time_1 = '';
				}
				if (this.personDetail.time_2) {
					this.personDetail.time_2 = this.$dayjs.unix(this.personDetail.time_2).format('YYYY-MM-DD');
				} else {
					this.personDetail.time_2 = '';
				}
			}
            // 获取用户信息
            if (this.vuex_wx_token) {
                let wxUserDetail = await this.$u.api.wxUserDetail();
                if (wxUserDetail) {
                    this.wxUserDetail = wxUserDetail;
                }
            }
            // 字段初始化
            for (let field in this.personFieldConfig) {
                if (this.personFieldConfig[field].hasOwnProperty('initFunc')) {
                    this.personFieldConfig[field].initFunc(this);
                }
            }
            this.pageInit = true;
        },
        async personSubmitFunc() {
            let result = await this.$u.api.planPersonUpdate(this.personDetail);
            if (!result) {
                return false;
            }
            this.$refs.uToast.show({
                title: '提交成功',
                type: 'success',
                url: '/pages/index'
            });
        }
    }
};
</script>

<style lang="scss">
.wo-page-body {
    background-color: #fff;
    border-radius: uni-unit(10px);
    &::after {
        border-radius: uni-unit(10px);
    }
}
</style>